package cn.cjlmonster.demo02.decoration;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.leochuan.ScaleLayoutManager;

/**
 * This project is Demo02
 * Created by cjl on 2021/11/7 9:44 下午.
 */

public class GradientIndicatorDecoration extends RecyclerView.ItemDecoration {

    private int colorActive = 0xFF890012;
    private int colorInactive = 0xFF1289AD;

    private static final float DP = Resources.getSystem().getDisplayMetrics().density;

    private final int mIndicatorHeight = (int) (DP * 200);

    private final float mIndicatorStrokeWidth = DP * 6;

    private final float mIndicatorItemLength = DP * 30;

    private final Paint mPaint;

    private LinearGradient gradientActive;
    private LinearGradient gradientInactive;

    int[] colorsActive = {Color.DKGRAY, Color.GREEN};
    int[] colorsInactive = {Color.RED,Color.YELLOW};

    public GradientIndicatorDecoration() {
        mPaint = new Paint();
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(mIndicatorStrokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);

        gradientActive = new LinearGradient(0, 0, 0, mIndicatorStrokeWidth,
                colorsActive, null, Shader.TileMode.MIRROR);

        gradientInactive = new LinearGradient(0, 0, 0, mIndicatorStrokeWidth,
                colorsInactive, null, Shader.TileMode.MIRROR);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.set(0, 0, 0, mIndicatorHeight);
    }

    private int currentPosition = 0;

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        int itemCount = parent.getAdapter().getItemCount();
        if (itemCount == 0) return;

        float totalLength = mIndicatorItemLength * itemCount;
        float indicatorStartX = (parent.getWidth() - totalLength) / 2F;
        float indicatorPosY = parent.getHeight() - mIndicatorHeight / 2F;
        drawInactiveIndicators(c, indicatorStartX, indicatorPosY, totalLength);

        ScaleLayoutManager layoutManager = (ScaleLayoutManager) parent.getLayoutManager();
        int activePosition = layoutManager.getCurrentPosition();
        if (activePosition != RecyclerView.NO_POSITION) {
            currentPosition = activePosition;
        }
        drawHighlights(c, indicatorStartX, indicatorPosY, currentPosition, itemCount);
    }

    private void drawInactiveIndicators(Canvas c, float indicatorStartX, float indicatorPosY, float totalLength) {
        mPaint.setStrokeCap(Paint.Cap.ROUND);
//        mPaint.setColor(colorInactive);
        mPaint.setShader(gradientInactive);

        c.drawLine(indicatorStartX, indicatorPosY, indicatorStartX + totalLength, indicatorPosY, mPaint);
    }

    private void drawHighlights(Canvas c, float indicatorStartX, float indicatorPosY, int highlightPosition, int itemCount) {
        mPaint.setStrokeCap(Paint.Cap.ROUND);
//        mPaint.setColor(colorActive);
        mPaint.setShader(gradientActive);

        float start = indicatorStartX + mIndicatorItemLength * highlightPosition;
        float end = start + mIndicatorItemLength;
        c.drawLine(start, indicatorPosY, end, indicatorPosY, mPaint);

        mPaint.setStrokeCap(Paint.Cap.BUTT);
//        mPaint.setColor(colorInactive);
        mPaint.setShader(gradientInactive);

        if (itemCount == 1) return;

        if (highlightPosition == 0) {
            start = end;
            end = end + mIndicatorStrokeWidth / 2;
            c.drawLine(start, indicatorPosY, end, indicatorPosY, mPaint);
        } else if(highlightPosition == itemCount - 1) {
            end = start;
            start = start - mIndicatorStrokeWidth / 2;
            c.drawLine(start, indicatorPosY, end, indicatorPosY, mPaint);
        } else {
            float start1 = start - mIndicatorStrokeWidth / 2;
            float end1 = start;
            c.drawLine(start1, indicatorPosY, end1, indicatorPosY, mPaint);
            float start2 = end;
            float end2 = end + mIndicatorStrokeWidth / 2;
            c.drawLine(start2, indicatorPosY, end2, indicatorPosY, mPaint);
        }
    }
}
